#!/bin/bash

# 日志函数
log() {
    echo "$(date +'%Y-%m-%d %H:%M:%S') - $1"
}

# 检查并安装必要的工具
install_required_tools() {
    log "检查并安装必要的工具..."

    # 检查并安装 Homebrew
    if ! command -v brew &> /dev/null; then
        log "Homebrew 未安装，正在安装..."
        /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)" || {
            log "Homebrew 安装失败，请检查网络连接或手动安装。"
            exit 1
        }
    fi

    # 检查并安装 curl
    if ! command -v curl &> /dev/null; then
        log "curl 未安装，正在安装..."
        if [[ "$OSTYPE" == "linux-gnu"* ]]; then
            sudo apt-get update
            sudo apt-get install -y curl || {
                log "curl 安装失败，请检查网络连接或手动安装。"
                exit 1
            }
        elif [[ "$OSTYPE" == "darwin"* ]]; then
            brew install curl || {
                log "curl 安装失败，请检查网络连接或手动安装。"
                exit 1
            }
        else
            log "不支持的操作系统。请手动安装 curl。"
            exit 1
        fi
    fi

    # 检查并安装 jq
    if ! command -v jq &> /dev/null; then
        log "jq 未安装，正在安装..."
        if [[ "$OSTYPE" == "linux-gnu"* ]]; then
            sudo apt-get update
            sudo apt-get install -y jq || {
                log "jq 安装失败，请检查网络连接或手动安装。"
                exit 1
            }
        elif [[ "$OSTYPE" == "darwin"* ]]; then
            brew install jq || {
                log "jq 安装失败，请检查网络连接或手动安装。"
                exit 1
            }
        else
            log "不支持的操作系统。请手动安装 jq。"
            exit 1
        fi
    fi

    # 检查 /opt/homebrew/bin/bash 是否存在
    if [[ ! -x "/opt/homebrew/bin/bash" ]]; then
        log "/opt/homebrew/bin/bash 未找到，正在安装..."
        if [[ "$OSTYPE" == "darwin"* ]]; then
            brew install bash || {
                log "bash 安装失败，请检查网络连接或手动安装。"
                exit 1
            }
        elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
            sudo apt-get update
            sudo apt-get install -y bash || {
                log "bash 安装失败，请检查网络连接或手动安装。"
                exit 1
            }
        else
            log "不支持的操作系统。请手动安装 bash。"
            exit 1
        fi
    fi


    log "必要工具已检查并安装完成。"
}

# 下载并执行指定的脚本
download_and_execute_scripts() {
    local base_url="https://gitee.com/vangelisx/hs_shell/raw/main/src"  # 写死的基准 URL
    local scripts=("dops.sh" "ob.sh" "of.sh" "ops_init.sh" "git-to-master.sh" "git-merge-master.sh" "git-clone-group.sh" "kp.sh")  # 在这里列出所有需要下载的脚本名称
    local download_dir="$HOME/Downloads"  # 用户的下载目录
    for script_name in "${scripts[@]}"; do
        local script_url="$base_url/$script_name"
        local script_path="$download_dir/$script_name"
        local script_name_no_ext="${script_name%.sh}"  # 去掉后缀 .sh
        log "下载脚本 $script_url 到 $script_path ..."
        curl -# -o "$script_path" "$script_url"
        log "请求sudo权限将脚本复制到 /usr/local/bin 并赋予执行权限..."
        sudo cp "$script_path" "/usr/local/bin/$script_name_no_ext"
        sudo chmod +x "/usr/local/bin/$script_name_no_ext"
        log "脚本已下载并复制到 /usr/local/bin，并赋予执行权限：/usr/local/bin/$script_name_no_ext"
    done
}


main() {
#    install_required_tools
    log "开始下载并执行脚本..."
    download_and_execute_scripts
    log "脚本执行完毕。"
}

main
